from django.db import models
from utils.models import BaseModel
from user.models import User
# Create your models here.

class MovieCategory(BaseModel):
    """分类
    跟电影是一对多的关系
    """
    name = models.CharField(max_length=64, unique=True, verbose_name="分类名称")
    class Meta:
        db_table = "movie_category"
        verbose_name = "电影分类"
        verbose_name_plural = verbose_name

    def __str__(self):
        return "%s" % self.name


class Movie(BaseModel):
    """课程"""
    movie_type = (
        (0, '免费'),
        (1, 'VIP专享'),
    )


    # 原始字段
    name = models.CharField(max_length=128, verbose_name="电影名称")
    movie_img = models.ImageField(upload_to="courses", max_length=255, verbose_name="封面图片", blank=True, null=True)
    movie_type = models.SmallIntegerField(choices=movie_type, default=0, verbose_name="付费类型")
    # 使用这个字段的原因
    brief = models.TextField(max_length=2048, verbose_name="详情介绍", null=True, blank=True)
    level = models.DecimalField(default=0,decimal_places=2,max_digits=3, verbose_name="推荐等级")
    pub_date = models.DateField(verbose_name="发布日期", auto_now_add=True)
    show_time = models.CharField(max_length=32, verbose_name='上映时间')
    attachment_path = models.FileField(upload_to="attachment", max_length=128, verbose_name="电影路径", blank=True, null=True)
    price = models.DecimalField(max_digits=6, decimal_places=2, verbose_name="电影价格", default=0)

    # 优化字段
    people = models.IntegerField(verbose_name="观看人数", default=0)

    # 关联字段
    director = models.ForeignKey('Director', on_delete=models.DO_NOTHING, null=True, blank=True, db_constraint=False,verbose_name="导演")
    performer = models.CharField(max_length=32, verbose_name="演员", null=True)
    movie_category = models.ForeignKey("MovieCategory", on_delete=models.SET_NULL, db_constraint=False, null=True, blank=True,verbose_name="电影分类")
    class Meta:
        db_table = "movie"
        verbose_name = "电影"
        verbose_name_plural = "电影"

    def __str__(self):
        return "%s" % self.name

    @property
    def course_type_name(self):
        return self.get_movie_type_display()

    @property
    def movie_categorys(self):
        return self.movie_category.name


# 导演表
class Director(BaseModel):
    name = models.CharField(max_length=32, verbose_name="导演名")

    class Meta:
        db_table = "director"
        verbose_name = "导演"
        verbose_name_plural = "导演"

    def __str__(self):
        return "%s" % self.name


class Comment(BaseModel):
    username = models.CharField(max_length=32, verbose_name="用户")
    movie_title = models.ForeignKey(to='Movie' ,on_delete=models.DO_NOTHING, null=True, blank=True, db_constraint=False,verbose_name="电影")
    content = models.CharField(verbose_name='评论内容', max_length=255)
    comment_time = models.DateTimeField(verbose_name='评论时间', auto_now_add=True)

    class Meta:
        db_table = "comment"
        verbose_name = "评论"
        verbose_name_plural = "评论"
